package com.cssw.fyzb.job.executor.service.user;

import com.cssw.fyzb.core.dao.acct.GzAddPriceDao;
import com.cssw.fyzb.core.dao.system.SysParamDao;
import com.cssw.fyzb.core.dao.team.UserTreeDao;
import com.cssw.fyzb.core.dao.user.UserDao;
import com.cssw.fyzb.core.dao.user.UserExtDao;
import com.cssw.fyzb.core.mao.task.DailyTaskMao;
import com.cssw.fyzb.core.mao.user.UserMao;
import com.cssw.fyzb.core.model.dodb.*;
import com.cssw.fyzb.core.model.mos.DailyTaskMo;
import com.cssw.fyzb.core.util.TimeUtil;
import com.ydn.dbframe.plugin.activerecord.Page;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;


/**
 * @description:
 * @author: yangyiqun
 * @time: 2021/2/7 15:49
 */
@Service
public class UserServiceImpl implements UserService {

    private static final Logger logger = LoggerFactory.getLogger(UserServiceImpl.class);

    @Autowired
    private GzAddPriceDao gzAddPriceDao;

    @Autowired
    private SysParamDao sysParamDao;

    @Autowired
    private UserDao userDao;

    @Autowired
    private UserExtDao userExtDao;

    @Autowired
    private UserMao userMao;

    @Override
    public void vipAddPrice() {
        int pageNumber = 1;
        int pageSize = 500;

        SysParam sysParam = sysParamDao.findByCode(SysParam.USER_VIP_ADD_PRICE);
        if (null == sysParam){
            logger.info("price is null.");
            return ;
        }

        BigDecimal vipAddPrice = new BigDecimal(sysParam.getParamValue());
        logger.info("begin vip add price job....");
        while (true) {
            Page<User> pages = userDao.pageVip(pageNumber, pageSize);
            if (pages.getList() == null || pages.getList().size() <= 0) {
                break;
            }

            pageNumber++;

            for (User user : pages.getList()) {
                try {
                    GzAddPrice gzAddPrice = gzAddPriceDao.getTodayVipAddPrice(user.getUserId());
                    if (null != gzAddPrice){
                        continue;
                    }

                    gzAddPriceDao.save(user.getUserId(), vipAddPrice, GzAddPrice.TASK_TYPE_VIP, user.getUserId().toString(),"VIP会员");
                    userExtDao.addGzAddPrice(user.getUserId(), vipAddPrice);

                } catch(Exception e){
                    logger.error("", e);
                    logger.info(">>>>vip add price:" + user.getUserId());
                }
            }
        }
        logger.info("end vip add price job....");
    }

    @Override
    public void userVipCheck() {

        int pageNumber = 1;
        int pageSize = 500;

        long currMills = System.currentTimeMillis();
        logger.info("user vip check job....");
        while (true) {
            Page<User> pages = userDao.pageVip(pageNumber, pageSize);
            if (pages.getList() == null || pages.getList().size() <= 0) {
                break;
            }
            pageNumber++;

            for (User user : pages.getList()) {
                try {
                    //非会员
                    if (user.getVip().equals("0")) {
                        continue;
                    }

                    if (null == user.getVipEndTime()) {
                        continue;
                    }

                    //会员时间到期
                    long vipEndMills = user.getVipEndTime().getTime();
                    if (currMills < vipEndMills) {
                        continue;
                    }


                    String userOpenVipTimeLong = sysParamDao.findByCode(SysParam.USER_OPEN_VIP_TIME_LONG).getParamValue();
                    userDao.updateVip(user.getUserId(), User.VIP_NO, userOpenVipTimeLong);
                    userMao.updateVip(user.getUserId(), User.VIP_NO);

                } catch(Exception e){
                    logger.error("", e);
                    logger.info(">>>>userVipCheckJob:" + user.getUserId());
                }
            }
        }
        logger.info("end user vip check job....");
    }
}
